import { t } from 'i18next'
import { useState } from 'react'
import { useTvWallStore } from '../store'
import { Modal } from '@kedacom/react-next'
import shallow from 'zustand/shallow'
import { fetchAddTvWallStyle, fetchDelTvWallStyle } from '@/api/manager/tvwall'
import { StyleInfoEntity } from '../AddStyleModal/hook'
import { MyUsedStyleEntity } from '../interface'
import { confirmModal } from '@/components/Alert'

export const useData = () => {
	const { update, meetingMoid, correct_style, getMccTVWallsByMeetingMoid, my_used_style, loadTvwallStyle } =
		useTvWallStore(
			(state) => ({
				update: state.update,
				meetingMoid: state.meetingMoid,
				correct_style: state.correct_style,
				my_used_style: state.my_used_style,
				getMccTVWallsByMeetingMoid: state.getMccTVWallsByMeetingMoid,
				loadTvwallStyle: state.loadTvwallStyle,
			}),
			shallow
		)
	/**新增风格样式模态框是否开启*/
	const [is_open, setIsOpen] = useState<boolean>(false)
	/**获取余下的电视墙风格*/
	const getRemainingMyUsedStyle = (_id: string): MyUsedStyleEntity => {
		let _result = my_used_style.filter((value) => {
			return value.id !== _id
		})
		return _result
	}
	/**删除电视墙风格 */
	const handleDeleteTVWallStyle = (item: { id: string; name: string }) => {
		confirmModal({
			msg: t('确定删除所选电视墙风格？'),
			onOk: async () => {
				let params = {
					styles: [item],
				}
				const success = await fetchDelTvWallStyle(meetingMoid, params)
				if (success) {
					const _id = item.id /**删除的风格id */
					const _my_used_style = getRemainingMyUsedStyle(_id) /**去除掉删除风格（会议余下使用的风格） */
					if (correct_style === _id) {
						//如果删除的是当前选中风格
						if (_my_used_style.length > 0) {
							/**重新加载第一个电视墙风格(一般默认是第一个) */
							const _correct_style = _my_used_style[0].id
							update({
								correct_style: _correct_style,
								my_used_style: _my_used_style,
								boxDetailVOsSource: [],
							})
							loadTvwallStyle(_correct_style)
						} else {
							/**清空所有的电视墙风格 */
							update({ my_used_style: [], correct_style: '', boxDetailVOsSource: [] })
						}
					} else {
						/**删除对应的电视墙风格 */
						update({ correct_style: correct_style, my_used_style: _my_used_style })
					}
				}
			},
		})
	}
	/**新增电视墙*/
	const handleAddCallBack = async (add_styles: Array<StyleInfoEntity>) => {
		if (add_styles.length > 0) {
			/**新增电视墙风格*/
			let _styles = add_styles
				? add_styles.map((item) => {
						return { id: item.id, name: item.name }
				  })
				: []
			/**新增的电视墙风格数据 */
			const params = {
				styles: _styles,
			}
			/**调用接口 */
			const success = await fetchAddTvWallStyle(meetingMoid, params)
			if (success) {
				var _arr = [...my_used_style, ..._styles]
				update({ my_used_style: _arr })
				getMccTVWallsByMeetingMoid(meetingMoid)
			} else {
				getMccTVWallsByMeetingMoid(meetingMoid)
			}
		} else {
			getMccTVWallsByMeetingMoid(meetingMoid)
		}
		update({ showUI: true })
	}
	/**切换电视墙风格*/
	const handleChangeStyle = (_id: string) => {
		if (correct_style === _id) return
		update({ correct_style: _id, boxDetailVOsSource: [], showUI: true })
		loadTvwallStyle(_id)
	}
	return {
		is_open,
		setIsOpen,
		/**删除电视墙风格*/
		handleDeleteTVWallStyle,
		/**新增电视墙风格 */
		handleAddCallBack,
		/**切换电视墙风格 */
		handleChangeStyle,
	}
}
